<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" type="text/css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" type="text/css">
    <link rel="stylesheet" href="./resources/prism/prism.css" type="text/css">
    <link rel="stylesheet" href="../css/ol.css" type="text/css">
    <link rel="stylesheet" href="./resources/layout.css" type="text/css">
    <script src="resources/mapbox-streets-v6-style.js"></script>
    <link rel="stylesheet" href="mapbox-vector-tiles-advanced.css">
    <script src="./resources/zeroclipboard/ZeroClipboard.min.js"></script>
    <title>Advanced Mapbox vector tiles example</title>
  </head>
  <body>

    <header class="navbar" role="navigation">
      <div class="container" id="navbar-inner-container">
        <a class="navbar-brand" href="./"><img src="./resources/logo-70x70.png">&nbsp;OpenLayers 3 Examples</a>
      </div>
    </header>

    <div class="container-fluid">

      <div class="row-fluid">
  <div class="span12">
    <div id="map" class="map"></div>
  </div>
</div>


      <div class="row-fluid">
        <div class="span12">
          <h4 id="title">Advanced Mapbox vector tiles example</h4>
          <p id="shortdesc">Example of a Mapbox vector tiles map with custom tile grid.</p>
          <div id="docs"><p>A vector tiles map which reuses the same tiles for subsequent zoom levels to save bandwith on mobile devices. <strong>Note</strong>: No map will be visible when the access token has expired.</p>
</div>
          <div id="tags">mapbox, vector, tiles, mobile</div>
          <div id="api-links">Related API documentation: <ul class="inline"><li><a href="../apidoc/ol.Attribution.html" title="API documentation for ol.Attribution">ol.Attribution</a></li>,<li><a href="../apidoc/ol.Map.html" title="API documentation for ol.Map">ol.Map</a></li>,<li><a href="../apidoc/ol.View.html" title="API documentation for ol.View">ol.View</a></li>,<li><a href="../apidoc/ol.format.MVT.html" title="API documentation for ol.format.MVT">ol.format.MVT</a></li>,<li><a href="../apidoc/ol.layer.VectorTile.html" title="API documentation for ol.layer.VectorTile">ol.layer.VectorTile</a></li>,<li><a href="../apidoc/ol.proj.html" title="API documentation for ol.proj">ol.proj</a></li>,<li><a href="../apidoc/ol.source.VectorTile.html" title="API documentation for ol.source.VectorTile">ol.source.VectorTile</a></li>,<li><a href="../apidoc/ol.style.Fill.html" title="API documentation for ol.style.Fill">ol.style.Fill</a></li>,<li><a href="../apidoc/ol.style.Icon.html" title="API documentation for ol.style.Icon">ol.style.Icon</a></li>,<li><a href="../apidoc/ol.style.Stroke.html" title="API documentation for ol.style.Stroke">ol.style.Stroke</a></li>,<li><a href="../apidoc/ol.style.Style.html" title="API documentation for ol.style.Style">ol.style.Style</a></li>,<li><a href="../apidoc/ol.style.Text.html" title="API documentation for ol.style.Text">ol.style.Text</a></li>,<li><a href="../apidoc/ol.tilegrid.TileGrid.html" title="API documentation for ol.tilegrid.TileGrid">ol.tilegrid.TileGrid</a></li></ul></div>
        </div>
      </div>

      <div class="row-fluid">
        <div id="source-controls">
          <a id="copy-button"><i class="fa fa-clipboard"></i> Copy</a>
          <a id="jsfiddle-button"><i class="fa fa-jsfiddle"></i> Edit</a>
        </div>
        <form method="POST" id="jsfiddle-form" target="_blank" action="http://jsfiddle.net/api/post/jquery/1.11.0/">
          <textarea class="hidden" name="js">var key &#x3D; &#x27;Your Mapbox access token from http://mapbox.com/ here&#x27;;

// For how many zoom levels do we want to use the same vector tiles?
// 1 means &quot;use tiles from all zoom levels&quot;. 2 means &quot;use the same tiles for 2
// subsequent zoom levels&quot;.
var reuseZoomLevels &#x3D; 2;

// Offset of loaded tiles from web mercator zoom level 0.
// 0 means &quot;At map zoom level 0, use tiles from zoom level 0&quot;. 1 means &quot;At map
// zoom level 0, use tiles from zoom level 1&quot;.
var zoomOffset &#x3D; 1;

// Calculation of tile urls
var resolutions &#x3D; [];
for (var z &#x3D; zoomOffset / reuseZoomLevels; z &lt;&#x3D; 22 / reuseZoomLevels; ++z) {
  resolutions.push(156543.03392804097 / Math.pow(2, z * reuseZoomLevels));
}
function tileUrlFunction(tileCoord) {
  return (&#x27;http://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v6/&#x27; +
      &#x27;{z}/{x}/{y}.vector.pbf?access_token&#x3D;&#x27; + key)
      .replace(&#x27;{z}&#x27;, String(tileCoord[0] * reuseZoomLevels + zoomOffset))
      .replace(&#x27;{x}&#x27;, String(tileCoord[1]))
      .replace(&#x27;{y}&#x27;, String(-tileCoord[2] - 1))
      .replace(&#x27;{a-d}&#x27;, &#x27;abcd&#x27;.substr(
          ((tileCoord[1] &lt;&lt; tileCoord[0]) + tileCoord[2]) % 4, 1));
}

var map &#x3D; new ol.Map({
  layers: [
    new ol.layer.VectorTile({
      preload: Infinity,
      source: new ol.source.VectorTile({
        attributions: [new ol.Attribution({
          html: &#x27;© &lt;a href&#x3D;&quot;https://www.mapbox.com/map-feedback/&quot;&gt;Mapbox&lt;/a&gt; &#x27; +
              &#x27;© &lt;a href&#x3D;&quot;http://www.openstreetmap.org/copyright&quot;&gt;&#x27; +
              &#x27;OpenStreetMap contributors&lt;/a&gt;&#x27;
        })],
        format: new ol.format.MVT(),
        tileGrid: new ol.tilegrid.TileGrid({
          extent: ol.proj.get(&#x27;EPSG:3857&#x27;).getExtent(),
          resolutions: resolutions
        }),
        tilePixelRatio: 16,
        tileUrlFunction: tileUrlFunction
      }),
      style: createMapboxStreetsV6Style()
    })
  ],
  target: &#x27;map&#x27;,
  view: new ol.View({
    center: [0, 0],
    minZoom: 1,
    zoom: 2
  })
});

// ol.style.Fill, ol.style.Icon, ol.style.Stroke, ol.style.Style and
// ol.style.Text are required for createMapboxStreetsV6Style()
</textarea>
          <textarea class="hidden" name="css">.map {
  background: #f8f4f0;
}
</textarea>
          <textarea class="hidden" name="html">&lt;div class&#x3D;&quot;row-fluid&quot;&gt;
  &lt;div class&#x3D;&quot;span12&quot;&gt;
    &lt;div id&#x3D;&quot;map&quot; class&#x3D;&quot;map&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
</textarea>
          <input type="hidden" name="wrap" value="l">
          <input type="hidden" name="resources" value="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css,https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js,http://openlayers.org/en/v3.11.1/css/ol.css,http://openlayers.org/en/v3.11.1/build/ol.js,http://openlayers.org/en/v3.11.1/examples/resources/mapbox-streets-v6-style.js">
        </form>
        <pre><code id="example-source" class="language-markup">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Advanced Mapbox vector tiles example&lt;/title&gt;
&lt;script src="https://code.jquery.com/jquery-1.11.2.min.js"&gt;&lt;/script&gt;
&lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"&gt;
&lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"&gt;&lt;/script&gt;
&lt;link rel="stylesheet" href="http://openlayers.org/en/v3.11.1/css/ol.css" type="text/css"&gt;
&lt;script src="http://openlayers.org/en/v3.11.1/build/ol.js"&gt;&lt;/script&gt;
&lt;script src&#x3D;&quot;http://openlayers.org/en/v3.11.1/examples/resources/mapbox-streets-v6-style.js&quot;&gt;&lt;/script&gt;
&lt;style&gt;
.map {
  background: #f8f4f0;
}

&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="container-fluid"&gt;

&lt;div class&#x3D;&quot;row-fluid&quot;&gt;
  &lt;div class&#x3D;&quot;span12&quot;&gt;
    &lt;div id&#x3D;&quot;map&quot; class&#x3D;&quot;map&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;script&gt;
var key &#x3D; &#x27;Your Mapbox access token from http://mapbox.com/ here&#x27;;

// For how many zoom levels do we want to use the same vector tiles?
// 1 means &quot;use tiles from all zoom levels&quot;. 2 means &quot;use the same tiles for 2
// subsequent zoom levels&quot;.
var reuseZoomLevels &#x3D; 2;

// Offset of loaded tiles from web mercator zoom level 0.
// 0 means &quot;At map zoom level 0, use tiles from zoom level 0&quot;. 1 means &quot;At map
// zoom level 0, use tiles from zoom level 1&quot;.
var zoomOffset &#x3D; 1;

// Calculation of tile urls
var resolutions &#x3D; [];
for (var z &#x3D; zoomOffset / reuseZoomLevels; z &lt;&#x3D; 22 / reuseZoomLevels; ++z) {
  resolutions.push(156543.03392804097 / Math.pow(2, z * reuseZoomLevels));
}
function tileUrlFunction(tileCoord) {
  return (&#x27;http://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v6/&#x27; +
      &#x27;{z}/{x}/{y}.vector.pbf?access_token&#x3D;&#x27; + key)
      .replace(&#x27;{z}&#x27;, String(tileCoord[0] * reuseZoomLevels + zoomOffset))
      .replace(&#x27;{x}&#x27;, String(tileCoord[1]))
      .replace(&#x27;{y}&#x27;, String(-tileCoord[2] - 1))
      .replace(&#x27;{a-d}&#x27;, &#x27;abcd&#x27;.substr(
          ((tileCoord[1] &lt;&lt; tileCoord[0]) + tileCoord[2]) % 4, 1));
}

var map &#x3D; new ol.Map({
  layers: [
    new ol.layer.VectorTile({
      preload: Infinity,
      source: new ol.source.VectorTile({
        attributions: [new ol.Attribution({
          html: &#x27;© &lt;a href&#x3D;&quot;https://www.mapbox.com/map-feedback/&quot;&gt;Mapbox&lt;/a&gt; &#x27; +
              &#x27;© &lt;a href&#x3D;&quot;http://www.openstreetmap.org/copyright&quot;&gt;&#x27; +
              &#x27;OpenStreetMap contributors&lt;/a&gt;&#x27;
        })],
        format: new ol.format.MVT(),
        tileGrid: new ol.tilegrid.TileGrid({
          extent: ol.proj.get(&#x27;EPSG:3857&#x27;).getExtent(),
          resolutions: resolutions
        }),
        tilePixelRatio: 16,
        tileUrlFunction: tileUrlFunction
      }),
      style: createMapboxStreetsV6Style()
    })
  ],
  target: &#x27;map&#x27;,
  view: new ol.View({
    center: [0, 0],
    minZoom: 1,
    zoom: 2
  })
});

// ol.style.Fill, ol.style.Icon, ol.style.Stroke, ol.style.Style and
// ol.style.Text are required for createMapboxStreetsV6Style()

&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
      </div>
    </div>

    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="./resources/common.js"></script>
    <script src="./resources/prism/prism.min.js"></script>
    <script src="loader.js?id=mapbox-vector-tiles-advanced"></script>

  </body>
</html>
